package com.btn.token.service.impl;

import com.btn.token.dao.UserDao;
import com.btn.token.model.entity.UserEntity;
import com.btn.token.model.view.UserView;
import com.btn.token.service.UserService;
import com.btn.token.utils.JwtUtils;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.swing.text.html.Option;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserView login(UserView userEntity) throws Exception {
//       Boolean userBl = userDao.existsByUsernameAndPassword(userEntity.getUsername(),userEntity.getPassword());
        UserEntity user = userDao.findAllByUsernameAndPassword(userEntity.getUsername(),userEntity.getPassword());
        UserView userView = new UserView();
        if (!ObjectUtils.isEmpty(user)){
            BeanUtils.copyProperties(user,userView);
           // 存在 获取token值保存到view中
            String token = JwtUtils.getToken(user);
            userView.setToken(token);
       }else {
           throw new Exception("用户账户密码错误");
       }
       return userView;
    }
}
